class Test {
    static main(args) {

        def numbers = [1,2,3,4]
        println (numbers + 5)
        println (numbers - [2,3])

        println "--------------------------------------"

        println (["Java", "Groovy"]*.toUpperCase())

        println "--------------------------------------"

        def nullObject = null
        println nullObject?.name

        println "--------------------------------------"

        ['a','b','c'].each{e->
            println e
        }
        ['a','b','c'].eachWithIndex{e,i->
            println "${i}:${e}"
        }

        [name:"张三",age:19].each{k,v->
            println "${k}:$v"
        }

        println "--------------------------------------"

        def result = "结果"

        //符合条件的元素会被挑选出来形成一个新的list
        result = ['a','ab','abc'].grep{e->
            e.length()<3
        }
        println "grep：" + result

        //条件以正则表达式形式传入，符合正则表达式的e会被提取出来形成一个新的list
        result = ['a','ab','abc'].grep(~/../)
        println "grep：" + result

        //条件以集合形式传入，相当于求两个集合的交集
        result = ['a','ab','abc'].grep(['a','cde','ab'])
        println "grep：" + result

        println "--------------------------------------"

        //只要有一个条件满足就返回true
        result = ['a','ab','abc'].any{e->
            e.length()<3
        }
        println "any：" + result

        //所有的都满足才返回true
        result = ['a','ab','abc'].every{e->
            e.length()<3
        }
        println "every：" + result

        println "--------------------------------------"

        //集合转字符串，以指定分隔符分隔
        result = [2017,4,24].join('-')
        println "join：" + result
        
        //字符串转集合，以指定符号为分隔符进行转换
        result = result.tokenize('-')
        println "tokenize：" + result
        
        println "--------------------------------------"

        //根据指定条件进行排序,e1-e2升序，e2-e1降序
        result = [2017,4,24].sort{e1,e2->
            return e1-e2
        }
        println "sort：" + result
        
        println "--------------------------------------"

        //查找集合中满足条件的第一个元素
        result = [2017,4,24].find{e->
            e<30
        }
        println "find：" + result
        
        //查找集合中满足条件的所有元素
        result = [2017,4,24].findAll{e->
            e<30
        }
        println "findAll：" + result
        
        println "--------------------------------------"

        //对集合中的元素进行处理，并将处理结果放到一个新的集合中
        result = ['a','b','c'].collect{e->
            e+'123'
        }
        println "collect：" + result
        
        //对map进行处理
        result = [name:'张三',age:18].collect{entry->
            "${entry.key}:${entry.value + 1}"
        }
        println "collect：" + result
        
        println "--------------------------------------"

        //对集合中的元素按给定的条件进行分组
        result = ['a','b','abc','ab','c','bc'].groupBy{e->
            e.length()
        }
        println "groupBy：" + result
        
        println "--------------------------------------"

        //将集合中的元素反转
        result = ['a','b','c'].reverse()
        println "reverse：" + result
        
        //反转字符串
        result = 'abc'.reverse()
        println "reverse：" + result
        
        println "--------------------------------------"

        //去除集合中重复的元素
        result = ['a','b','b','a','c'].unique()
        println "unique：" + result
        
        println "--------------------------------------"

        //求最大值和最小值
        println ([1,2,3].max())
        println ([1,2,3].min())

        //按照比较的内容在集合中选出最大的元素
        result = ['a','ab','abc'].max{e->
            e.length()
        }
        println "max：" + result
        
        println "--------------------------------------"
        
        //计数
        result = ['a','b','a','c','a'].count('a')
        println "count：" + result
        
        println "--------------------------------------"

        //求和
        result = [1,2,3].sum()
        println "sum：" + result
        
        //求字符串的和，就是连接字符串
        result = ['a','b','c'].sum()
        println "sum：" + result

    }

}
